package com.appspot.inkisapi;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.inkisapi.service.SimpleDatastoreService;

public class SimpleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String name = req.getParameter("name");
		String content = req.getParameter("content");
		Map<String, Object> properties = new HashMap<String, Object>();
		properties.put("content", content);
		SimpleDatastoreService.createOrUpdateEntity("Article", name,
				properties);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String key = req.getParameter("key");
		resp.setContentType("text/javascript");
		String result = SimpleDatastoreService.getEntity("Article", key).getProperty("content").toString();
		String jsonp = "callback" +"({content:"+result+"});";
		resp.getWriter().println(jsonp);
	}
	
	@Override
	protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doDelete(req, resp);
	}
}
